setwd("/Users/msbell/Documents/My Documents/Minnesota/Work/Research/Security Dilemma/Publication version/Replication/")
library(gplots)
China23<-read.csv("Ch_2023_for_analysis.csv")
US23<-read.csv("US_2023_for_analysis.csv")


China23$hiint<-c(1)
for(i in 1:nrow(China23)){
	if(!is.na(China23$interest[i]) & (China23$interest[i]>=median(China23$interest, na.rm=T)) ) {China23$hiint[i]<-2} else{}  
	print(i)
}

US23$hiint<-c(1)
for(i in 1:nrow(US23)){
	if(!is.na(US23$interest[i]) & (US23$interest[i]>=median(US23$interest, na.rm=T)) ) {US23$hiint[i]<-2} else{}  
	print(i)
}

China23$hiknow<-c(1)
for(i in 1:nrow(China23)){
	if(!is.na(China23$pol.know[i]) & (China23$pol.know[i]>=median(China23$pol.know, na.rm=T)) ) {China23$hiknow[i]<-2} else{}  
	print(i)
}

US23$hiknow<-c(1)
for(i in 1:nrow(US23)){
	if(!is.na(US23$pol.know[i]) & (US23$pol.know[i]>=median(US23$pol.know, na.rm=T)) ) {US23$hiknow[i]<-2} else{}  
	print(i)
}



USgCh<-US23[US23$USgivenCh==1,]
USgUS<-US23[US23$USgivenUS==1,]

ChgCh<-China23[China23$ChgivenCh==1,]
ChgUS<-China23[China23$ChgivenUS==1,]


#Ch asymmetries for off/def
#overall
asym1<-t.test(ChgUS$USdef, ChgCh$Chdef, var.equal=F)

#gender
asym2<-t.test(ChgUS[ChgUS$gender==1,]$USdef, ChgCh[ChgCh$gender==1,]$Chdef, var.equal=F)
asym3<-t.test(ChgUS[ChgUS$gender==2,]$USdef, ChgCh[ChgCh$gender==2,]$Chdef, var.equal=F)

#CCP
asym4<-t.test(ChgUS[ChgUS$CCP==1,]$USdef, ChgCh[ChgCh$CCP==1,]$Chdef, var.equal=F)
asym5<-t.test(ChgUS[ChgUS$CCP==0,]$USdef, ChgCh[ChgCh$CCP==0,]$Chdef, var.equal=F)

#hi-int
asym6<-t.test(ChgUS[ChgUS$hiint==2,]$USdef, ChgCh[ChgCh$hiint==2,]$Chdef, var.equal=F)
asym7<-t.test(ChgUS[ChgUS$hiint==1,]$USdef, ChgCh[ChgCh$hiint==1,]$Chdef, var.equal=F)


#hi-know
asym8<-t.test(ChgUS[ChgUS$hiknow==2,]$USdef, ChgCh[ChgCh$hiknow==2,]$Chdef, var.equal=F)
asym9<-t.test(ChgUS[ChgUS$hiknow==1,]$USdef, ChgCh[ChgCh$hiknow==1,]$Chdef, var.equal=F)


#Ch asymmetries for threat
#overall
asym10<-t.test(ChgUS$USisthreat, ChgCh$Chisthreat, var.equal=F)

#gender
asym11<-t.test(ChgUS[ChgUS$gender==1,]$USisthreat, ChgCh[ChgCh$gender==1,]$Chisthreat, var.equal=F)
asym12<-t.test(ChgUS[ChgUS$gender==2,]$USisthreat, ChgCh[ChgCh$gender==2,]$Chisthreat, var.equal=F)

#CCP
asym13<-t.test(ChgUS[ChgUS$CCP==1,]$USisthreat, ChgCh[ChgCh$CCP==1,]$Chisthreat, var.equal=F)
asym14<-t.test(ChgUS[ChgUS$CCP==0,]$USisthreat, ChgCh[ChgCh$CCP==0,]$Chisthreat, var.equal=F)

#hi-int
asym15<-t.test(ChgUS[ChgUS$hiint==2,]$USisthreat, ChgCh[ChgCh$hiint==2,]$Chisthreat, var.equal=F)
asym16<-t.test(ChgUS[ChgUS$hiint==1,]$USisthreat, ChgCh[ChgCh$hiint==1,]$Chisthreat, var.equal=F)

#hi-know
asym17<-t.test(ChgUS[ChgUS$hiknow==2,]$USisthreat, ChgCh[ChgCh$hiknow==2,]$Chisthreat, var.equal=F)
asym18<-t.test(ChgUS[ChgUS$hiknow==1,]$USisthreat, ChgCh[ChgCh$hiknow==1,]$Chisthreat, var.equal=F)


#US asymmetries for off/def
#overall
usasym1<-t.test(USgCh$Chdef, USgUS$USdef, var.equal=F)

#gender
usasym2<-t.test(USgCh[USgCh$gender==1,]$Chdef, USgUS[USgUS$gender==1,]$USdef, var.equal=F)
usasym3<-t.test(USgCh[USgCh$gender==2,]$Chdef, USgUS[USgUS$gender==2,]$USdef, var.equal=F)

#partisan
usasym4<-t.test(USgCh[USgCh$republican==1,]$Chdef, USgUS[USgUS$republican==1,]$USdef, var.equal=F)
usasym5<-t.test(USgCh[USgCh$republican==0,]$Chdef, USgUS[USgUS$republican==0,]$USdef, var.equal=F)

#hi-int
usasym6<-t.test(USgCh[USgCh$hiint==2,]$Chdef, USgUS[USgUS$hiint==2,]$USdef, var.equal=F)
usasym7<-t.test(USgCh[USgCh$hiint==1,]$Chdef, USgUS[USgUS$hiint==1,]$USdef, var.equal=F)


#hi-know
usasym8<-t.test(USgCh[USgCh$hiknow==2,]$Chdef, USgUS[USgUS$hiknow==2,]$USdef, var.equal=F)
usasym9<-t.test(USgCh[USgCh$hiknow==1,]$Chdef, USgUS[USgUS$hiknow==1,]$USdef, var.equal=F)


#US asymmetries for threat
#overall
usasym10<-t.test(USgCh$Chisthreat, USgUS$USisthreat, var.equal=F)

#gender
usasym11<-t.test(USgCh[USgCh$gender==1,]$Chisthreat, USgUS[USgUS$gender==1,]$USisthreat, var.equal=F)
usasym12<-t.test(USgCh[USgCh$gender==2,]$Chisthreat, USgUS[USgUS$gender==2,]$USisthreat, var.equal=F)

#partisan
usasym13<-t.test(USgCh[USgCh$republican ==1,]$Chisthreat, USgUS[USgUS$republican ==1,]$USisthreat, var.equal=F)
usasym14<-t.test(USgCh[USgCh$republican ==0,]$Chisthreat, USgUS[USgUS$republican ==0,]$USisthreat, var.equal=F)

#hi-int
usasym15<-t.test(USgCh[USgCh$hiint==2,]$Chisthreat, USgUS[USgUS$hiint==2,]$USisthreat, var.equal=F)
usasym16<-t.test(USgCh[USgCh$hiint==1,]$Chisthreat, USgUS[USgUS$hiint==1,]$USisthreat, var.equal=F)

#hi-know
usasym17<-t.test(USgCh[USgCh$hiknow==2,]$Chisthreat, USgUS[USgUS$hiknow==2,]$USisthreat, var.equal=F)
usasym18<-t.test(USgCh[USgCh$hiknow==1,]$Chisthreat, USgUS[USgUS$hiknow==1,]$USisthreat, var.equal=F)





pdf("asym_bar_appendix.pdf", height=18, width=10)

par(mfrow=c(4,1))


matrix_asym<-matrix(data=c(asym2$estimate[1]-asym2$estimate[2], asym3$estimate[1]-asym3$estimate[2], asym4$estimate[1]-asym4$estimate[2], asym5$estimate[1]-asym5$estimate[2], asym6$estimate[1]-asym6$estimate[2], asym7$estimate[1]-asym7$estimate[2], asym8$estimate[1]-asym8$estimate[2], asym9$estimate[1]-asym9$estimate[2]), ncol=4, byrow=F)

lower_asym<-matrix(data=c(asym2$conf.int[1],asym3$conf.int[1],asym4$conf.int[1],asym5$conf.int[1],asym6$conf.int[1],asym7$conf.int[1],asym8$conf.int[1],asym9$conf.int[1]), ncol=4, byrow=F)

upper_asym<-matrix(data=c(asym2$conf.int[2],asym3$conf.int[2],asym4$conf.int[2],asym5$conf.int[2],asym6$conf.int[2],asym7$conf.int[2],asym8$conf.int[2],asym9$conf.int[2]), ncol=4, byrow=F)

barplot2(matrix_asym, beside=T, col=c("GRAY30", "GRAY45"), space=c(0,1.5), ylim=c(0,2.5), axes=T, ylab="Asymmetry in assessment", names.arg=c("Gender", "CCP member", "Pol. interest", "Pol. knowledge"), plot.ci=T, ci.l=lower_asym, ci.u=upper_asym, xpd=F, cex=1, cex.lab=1.4, cex.names=1.7, cex.main=1.8, main= "Asymmetries in perceptions of offensive intentions (China, 2023)")

segments(x0=1.2, x1=15, y0=asym1$estimate[1]-asym1$estimate[2], y1=asym1$estimate[1]-asym1$estimate[2], lty=3, col="BLACK", lwd=1.5)

legend(x=10, y=2.3, lty=c(3), lwd=c(1.5), col=c("BLACK"), legend=c("Asymmetry in overall sample" ), bty="n", cex=1.1)

text(x=c(2,3,5.5,6.5,9,10,12.5,13.5), y=0.25, col="WHITE" , cex=1, labels=c("Male", "Female", "Yes", "No", "High", "Low", "High", "Low"))



matrix_asym<-matrix(data=c(asym11$estimate[1]-asym11$estimate[2], asym12$estimate[1]-asym12$estimate[2], asym13$estimate[1]-asym13$estimate[2], asym14$estimate[1]-asym14$estimate[2], asym15$estimate[1]-asym15$estimate[2], asym16$estimate[1]-asym16$estimate[2], asym17$estimate[1]-asym17$estimate[2], asym18$estimate[1]-asym18$estimate[2]), ncol=4, byrow=F)

lower_asym<-matrix(data=c(asym11$conf.int[1],asym12$conf.int[1],asym13$conf.int[1],asym14$conf.int[1],asym15$conf.int[1],asym16$conf.int[1],asym17$conf.int[1],asym18$conf.int[1]), ncol=4, byrow=F)

upper_asym<-matrix(data=c(asym11$conf.int[2],asym12$conf.int[2],asym13$conf.int[2],asym14$conf.int[2],asym15$conf.int[2],asym16$conf.int[2],asym17$conf.int[2],asym18$conf.int[2]), ncol=4, byrow=F)

barplot2(matrix_asym, beside=T, col=c("GRAY30", "GRAY45"), space=c(0,1.5), ylim=c(0,2.5), axes=T, ylab="Asymmetry in assessment", names.arg=c("Gender", "CCP member", "Pol. interest", "Pol. knowledge"), plot.ci=T, ci.l=lower_asym, ci.u=upper_asym, xpd=F, cex=1, cex.lab=1.4, cex.names=1.7, cex.main=1.8, main= "Asymmetries in perceptions of threat (China, 2023)")

segments(x0=1.2, x1=15, y0=asym10$estimate[1]-asym10$estimate[2], y1=asym10$estimate[1]-asym10$estimate[2], lty=3, col="BLACK", lwd=1.5)

legend(x=10, y=2.3, lty=c(3), lwd=c(1.5), col=c("BLACK"), legend=c("Asymmetry in overall sample" ), bty="n", cex=1.1)

text(x=c(2,3,5.5,6.5,9,10,12.5,13.5), y=0.25, col="WHITE" , cex=1, labels=c("Male", "Female", "Yes", "No", "High", "Low", "High", "Low"))


matrix_usasym<-matrix(data=c(usasym2$estimate[1]-usasym2$estimate[2], usasym3$estimate[1]-usasym3$estimate[2], usasym4$estimate[1]-usasym4$estimate[2], usasym5$estimate[1]-usasym5$estimate[2], usasym6$estimate[1]-usasym6$estimate[2], usasym7$estimate[1]-usasym7$estimate[2], usasym8$estimate[1]-usasym8$estimate[2], usasym9$estimate[1]-usasym9$estimate[2]), ncol=4, byrow=F)

lower_usasym<-matrix(data=c(usasym2$conf.int[1],usasym3$conf.int[1],usasym4$conf.int[1],usasym5$conf.int[1],usasym6$conf.int[1],usasym7$conf.int[1],usasym8$conf.int[1],usasym9$conf.int[1]), ncol=4, byrow=F)

upper_usasym<-matrix(data=c(usasym2$conf.int[2],usasym3$conf.int[2],usasym4$conf.int[2],usasym5$conf.int[2],usasym6$conf.int[2],usasym7$conf.int[2],usasym8$conf.int[2],usasym9$conf.int[2]), ncol=4, byrow=F)

barplot2(matrix_usasym, beside=T, col=c("GRAY30", "GRAY45"), space=c(0,1.5), ylim=c(0,2.5), axes=T, ylab="Asymmetry in assessment", names.arg=c("Gender", "Republican", "Pol. interest", "Pol. knowledge"), plot.ci=T, ci.l=lower_usasym, ci.u=upper_usasym, xpd=F, cex=1, cex.lab=1.4, cex.names=1.7, cex.main=1.8, main= "Asymmetries in perceptions of threat (USA, 2023)")

segments(x0=1.2, x1=15, y0=usasym1$estimate[1]-usasym1$estimate[2], y1=usasym1$estimate[1]-usasym1$estimate[2], lty=3, col="BLACK", lwd=1.5)

text(x=c(2,3,5.5,6.5,9,10,12.5,13.5), y=0.1, col="WHITE" , cex=1, labels=c("Male", "Female", "Yes", "No", "High", "Low", "High", "Low"))


legend(x=10, y=2.3, lty=c(3), lwd=c(1.5), col=c("BLACK"), legend=c("Asymmetry in overall sample" ), bty="n", cex=1.1)


matrix_usasym<-matrix(data=c(usasym11$estimate[1]-usasym11$estimate[2], usasym12$estimate[1]-usasym12$estimate[2], usasym13$estimate[1]-usasym13$estimate[2], usasym14$estimate[1]-usasym14$estimate[2], usasym15$estimate[1]-usasym15$estimate[2], usasym16$estimate[1]-usasym16$estimate[2], usasym17$estimate[1]-usasym17$estimate[2], usasym18$estimate[1]-usasym18$estimate[2]), ncol=4, byrow=F)

lower_usasym<-matrix(data=c(usasym11$conf.int[1],usasym12$conf.int[1],usasym13$conf.int[1],usasym14$conf.int[1],usasym15$conf.int[1],usasym16$conf.int[1],usasym17$conf.int[1],usasym18$conf.int[1]), ncol=4, byrow=F)

upper_usasym<-matrix(data=c(usasym11$conf.int[2],usasym12$conf.int[2],usasym13$conf.int[2],usasym14$conf.int[2],usasym15$conf.int[2],usasym16$conf.int[2],usasym17$conf.int[2],usasym18$conf.int[2]), ncol=4, byrow=F)

barplot2(matrix_usasym, beside=T, col=c("GRAY30", "GRAY45"), space=c(0,1.5), ylim=c(0,2.5), axes=T, ylab="Asymmetry in assessment", names.arg=c("Gender", "Republican", "Pol. interest", "Pol. knowledge"), plot.ci=T, ci.l=lower_usasym, ci.u=upper_usasym, xpd=F, cex=1, cex.lab=1.4, cex.names=1.7, cex.main=1.8, main= "Asymmetries in perceptions of threat (USA, 2023)")

text(x=c(2,3,5.5,6.5,9,10,12.5,13.5), y=0.2, col="WHITE" , cex=1, labels=c("Male", "Female", "Yes", "No", "High", "Low", "High", "Low"))

segments(x0=1.2, x1=15, y0=usasym10$estimate[1]-usasym10$estimate[2], y1=usasym10$estimate[1]-usasym10$estimate[2], lty=3, col="BLACK", lwd=1.5)

legend(x=10, y=2.3, lty=c(3), lwd=c(1.5), col=c("BLACK"), legend=c("Asymmetry in overall sample" ), bty="n", cex=1.1)




dev.off()



